package com.bootdo.system.controller;

import com.bootdo.common.controller.BaseController;
import com.bootdo.common.domain.DictDO;
import com.bootdo.common.utils.DictUtils;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.system.dao.UserRoleDao;
import com.bootdo.system.domain.IntegralLogDO;
import com.bootdo.system.domain.UserDO;
import com.bootdo.system.service.IntegralLogService;
import com.bootdo.system.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 
 * 
 * @author huizhang
 * @email 
 * @date 2021-04-08 15:27:14
 */
 
@Controller
@RequestMapping("/system/integralLog")
public class IntegralLogController extends BaseController {

	@Autowired
	private UserRoleDao userRoleDao;

	@Autowired
	private IntegralLogService logService;

	@Autowired
	UserService userService;
	
	@GetMapping()
	String Log(){
	    return "system/integralLog/log";
	}
	
	@ResponseBody
	@GetMapping("/list")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
		//获取用户角色
		List<Long> list = userRoleDao.listRoleId(getUserId());
		if(!CollectionUtils.isEmpty(list)) {
			if(!list.contains(1l)) {
				//普通用户
				params.put("userId",getUserId());
			}
		}
        Query query = new Query(params);
		List<IntegralLogDO> logList = logService.list(query);
		int total = logService.count(query);
		PageUtils pageUtils = new PageUtils(logList, total);
		return pageUtils;
	}
	
	@GetMapping("/add/{id}")
	String add(@PathVariable("id") Long id, Model model){
		UserDO toUserDO = userService.get(id);
		model.addAttribute("toUserDO",toUserDO);
		model.addAttribute("fromUserDO",getUser());
	    return "system/integralLog/add";
	}

	@GetMapping("/edit/{id}")
	String edit(@PathVariable("id") Long id,Model model){
		IntegralLogDO log = logService.get(id);
		model.addAttribute("log", log);
	    return "system/integralLog/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public R save(IntegralLogDO log){
		R r = logService.save(log);
		return r;
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("system:log:edit")
	public R update( IntegralLogDO log){
		logService.update(log);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("system:log:remove")
	public R remove( Long id){
		if(logService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("system:log:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		logService.batchRemove(ids);
		return R.ok();
	}
	
}
